<?php
class Authorable extends Doctrine_Template {

  protected $_options = array(
    'author' => array(
         'column' => 'author_id',
         'alias' => 'Author',
         'enabled' => true,
         'method' => 'getId',
      ),
    'author_name' => array(
      'column' => 'author_name',
      'enabled' => true,
      'method' => '__toString',
      'default_name' => 'Anonymous',
    ),
    'modifier' => array(
        'column' => 'modifier_id',
        'alias' => 'Modifier',
        'enabled' => false, // default is false
        'method' => 'getId'
    ),
    'modifier_name' => array(
      'column' => 'modifier_name',
      'enabled' => false, // default is false
      'method' => '__toString',
      'default_name' => 'Anonymous',
    )
  );

  public function __construct(array $options = array()){
    $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
  }

  public function setTableDefinition() {
    if ($this->_options['author']['enabled']){
      $this->hasColumn($this->_options['author']['column'], 'integer', 4);
    }
    if ($this->_options['author_name']['enabled']){
      $this->hasColumn(
              $this->_options['author_name']['column'],
              'string',
              255,
              array('type' => 'string', 'length' => '255')
            );
    }
    if ($this->_options['modifier']['enabled']){
      $this->hasColumn($this->_options['modifier']['column'], 'integer', 4);
    }
    if ($this->_options['modifier_name']['enabled']){
      $this->hasColumn(
              $this->_options['modifier_name']['column'],
              'string',
              255,
              array('type' => 'string', 'length' => '255')
            );
    }
    $this->addListener(new Doctrine_Template_Listener_Authorable($this->_options));
  }

  

  public function setUp(){
    if ($this->_options['author']['enabled']){
     $this->hasOne('sfGuardUser as ' . $this->_options['author']['alias'], array(
      'local' => $this->_options['author']['column'],
      'foreign' => 'id',
      'onDelete' => 'SET NULL'
      ));
    }
    if ($this->_options['modifier']['enabled']){
      $this->hasOne('sfGuardUser as '  . $this->_options['modifier']['alias'], array(
        'local' => $this->_options['modifier']['column'],
        'foreign' => 'id',
        'onDelete' => 'SET NULL'
        ));
    }
  }
}